package com.briup.ptp.web.controller;

import ch.qos.logback.core.util.FileUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.briup.ptp.entity.Pig;
import com.briup.ptp.entity.extend.PigExtent;
import com.briup.ptp.service.PigService;
import com.briup.ptp.utils.Result;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @author GaoB
 * @date 2024/2/20 10:36:35
 * @description 生猪信息控制层
 */
@RestController
@RequestMapping("/pig")
@Api(tags = "生猪管理")
@CrossOrigin
public class PigController {
    @Autowired
    PigService service;

    @GetMapping("/query")
    @ApiOperation("分页+条件查询生猪信息")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name="species",value="生猪的种类"),
                    @ApiImplicitParam(name="name",value="病症的名称",readOnly = true),
                    @ApiImplicitParam(name="pageSize",value="分页每页数据条数",required = true),
                    @ApiImplicitParam(name="pageNum",value="页码",required = true)
            }
    )
    public Result queryPigInfo(String species, String name, Integer pageSize, Integer pageNum){
        List<PigExtent> pigs = service.queryPigInfo(species, name, pageSize, pageNum);
        return Result.success(pigs);
    }

    @GetMapping("/findByPigId")
    @ApiOperation("根据ID查询生猪信息")
    @ApiImplicitParam(name = "id",value = "生猪的id",required = true)
    public Result findByPigId(Integer id){
        return Result.success(service.getOne(new QueryWrapper<>(new Pig().setId(id))));
    }

    @GetMapping("/QRcode/{id}")
    @ApiOperation("根据生猪ID生成二维码")
    public Result QRcode (@PathVariable Integer id){
        return Result.success(service.QRcode(id));
    }

    @PostMapping("/saveOrUpdate")
    @ApiOperation("新增或修改生猪信息")
    @ApiImplicitParam(name="pig",value="生猪对象",required = true)
    public Result saveOrUpdate(@RequestBody Pig pig){
        service.saveOrUpdate(pig);
        return Result.success();
    }

    @DeleteMapping("/deleteById/{id}")
    @ApiOperation("根据ID删除生猪的信息")
    @ApiImplicitParam(name= "id",value = "生猪的id",required = true)
    public Result deleteById(@PathVariable Integer id){
        service.removeById(id);
        return Result.success();
    }

    @DeleteMapping("/deleteByIdAll")
    @ApiOperation("根据多个ID删除多个生猪信息")
    @ApiImplicitParam(name= "ids",value = "多个生猪的id",required = true)
    public Result deleteByIdAll(@RequestBody ArrayList<Integer> ids){
        return Result.success(service.removeBatchByIds(ids));
    }
}
